#!/bin/bash
# $1=IDE_ROOT, IDE根目录路径
# $2=CFG_PATH, msl配置项目路径
# $3=CONFIG, 配置名称
# $4=PRJ_NAME, msl项目名称
# $5=RPORT，监听端口
# $6=IP_ADDR 
# $7=MAC_ADDR
# $8=NA 网络适配器名称
# $9=VIP 网络适配器IP
# $10=VMASK 网络适配器子网掩码
# $11=VGATEWAY 网络适配器网关
# $12=IS_LINUX
IDE_ROOT=$1
CFG_PATH=$2
CONFIG=$3
PRJ_NAME=$4
RPORT=$5
IP_ADDR=$6
MAC_ADDR=$7
NA=$8
VIP=$9
VMASK=${10}
VGATEWAY=${11}
IS_LINUX=${12}

if [ "$IS_LINUX" == "true" ]; then
    echo $VIP > ~/ACoreIDE_nIP.txt
    UP_AND_DOWN="script=$IDE_ROOT/host/pub/qemu/etc/qemu-ifup,downscript=$IDE_ROOT/host/pub/qemu/etc/qemu-ifdown"
    if [ "$CONFIG" == "cortexA9_mc" ]; then
        $IDE_ROOT/host/pub/qemu/qemu-6.4.0/bin/qemu-system-aarch64 -machine vexpress-a9 -m 1000M -kernel $CFG_PATH/$PRJ_NAME.elf -smp 2 -serial tcp:127.0.0.1:$RPORT -net nic,addr=$IP_ADDR,macaddr=$MAC_ADDR -net tap,$UP_AND_DOWN
    elif [ "$CONFIG" == "ls2k1000_64_mc" ] || [ "$CONFIG" == "ls2k1000_mc" ]; then
        $IDE_ROOT/host/pub/qemu/qemu-2.12.96/bin/qemu-system-mips64el -machine ls2k -m 1000M -kernel $CFG_PATH/$PRJ_NAME.elf -smp 2 -serial tcp:127.0.0.1:$RPORT -net nic,addr=$IP_ADDR,macaddr=$MAC_ADDR -net tap,$UP_AND_DOWN
    fi
else
    netsh interface ip set address name="$NA" static $VIP $VMASK $VGATEWAY
    if [ "$CONFIG" == "cortexA9_mc" ]; then
        $IDE_ROOT/host/pub/qemu/qemu-2.7.0/qemu-system-aarch64.exe -machine vexpress-a9 -m 1000M -kernel $CFG_PATH/$PRJ_NAME.elf -smp 2 -serial tcp:127.0.0.1:$RPORT -net nic,addr=$IP_ADDR,macaddr=$MAC_ADDR -net tap,ifname=$NA
    elif [ "$CONFIG" == "ft2000aC4_64_qemu_mc" ] || [ "$CONFIG" == "ft2000aC4_qemu_mc" ]; then
        if [ "$CONFIG" == "ft2000aC4_64_qemu_mc" ] ; then
            DATA_FILE="datafile-yuan64.bin"
        else
            DATA_FILE="datafile-yuan.bin"
        fi
        echo "$IDE_ROOT/host/pub/qemu/qemu-5.1.0/bin/qemu-system-aarch64.exe -M virt_armdp,highmem=off,gic-version=3,dataFile="$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/$DATA_FILE",loadCfgFile="$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/loadfiles.yuan.cfg",rfBase=0x20000000,rfSize=0x2000000,ramfile=$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/nvram.ram -cpu cortex-a72 -smp cpus=4 -m 4G -accel tcg,thread=multi -kernel $CFG_PATH/$PRJ_NAME.elf -netdev tap,ifname=$NA,id=nd0 -serial null -serial tcp:127.0.0.1:$RPORT"
        $IDE_ROOT/host/pub/qemu/qemu-5.1.0/bin/qemu-system-aarch64.exe -M virt_armdp,highmem=off,gic-version=3,dataFile="$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/$DATA_FILE",loadCfgFile="$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/loadfiles.yuan.cfg",rfBase=0x20000000,rfSize=0x2000000,ramfile=$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/nvram.ram -cpu cortex-a72 -smp cpus=4 -m 4G -accel tcg,thread=multi -kernel $CFG_PATH/$PRJ_NAME.elf -netdev tap,ifname=$NA,id=nd0 -serial null -serial tcp:127.0.0.1:$RPORT
    elif [ "$CONFIG" == "armv8a_64_sc" ] || [ "$CONFIG" == "armv8a_sc" ]; then
        if [ "$CONFIG" == "armv8a_64_sc" ] ; then
            DATA_FILE="datafile-yuan64.bin"
        else
            DATA_FILE="datafile-yuan.bin"
        fi
        echo "$IDE_ROOT/host/pub/qemu/qemu-5.1.0/bin/qemu-system-aarch64.exe -M virt_armdp,highmem=off,gic-version=3,dataFile="$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/$DATA_FILE",loadCfgFile="$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/loadfiles.yuan.cfg",rfBase=0x20000000,rfSize=0x2000000,ramfile=$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/nvram.ram -cpu cortex-a72 -smp cpus=4 -m 4G -accel tcg,thread=multi -kernel $CFG_PATH/$PRJ_NAME.elf -netdev tap,ifname=$NA,id=nd0 -serial null -serial tcp:127.0.0.1:$RPORT"
        $IDE_ROOT/host/pub/qemu/qemu-5.1.0/bin/qemu-system-aarch64.exe -M virt_armdp,highmem=off,gic-version=3,dataFile="$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/$DATA_FILE",loadCfgFile="$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/loadfiles.yuan.cfg",rfBase=0x20000000,rfSize=0x2000000,ramfile=$IDE_ROOT/host/pub/qemu/qemu-5.1.0/config/arm/nvram.ram -cpu cortex-a72 -smp cpus=4 -m 4G -accel tcg,thread=multi -kernel $CFG_PATH/$PRJ_NAME.elf -netdev tap,ifname=$NA,id=nd0 -serial null -serial tcp:127.0.0.1:$RPORT
    elif [ "$CONFIG" == "ls2k1000_64_mc" ] || [ "$CONFIG" == "ls2k1000_mc" ]; then
        $IDE_ROOT/host/pub/qemu/qemu-2.12.96/qemu-system-mips64el.exe -machine ls2k -accel tcg,thread=multi -m 1000M -kernel $CFG_PATH/$PRJ_NAME.elf -smp 2 -serial tcp:127.0.0.1:$RPORT -net nic,addr=$IP_ADDR,macaddr=$MAC_ADDR -net tap,ifname=$NA
    elif [ "$CONFIG" == "riscv64be_sc" ]; then
        $IDE_ROOT/host/pub/qemu/qemu-5.1.0/bin/qemu-system-riscv64.exe -machine virt -m 512M -kernel $CFG_PATH/$PRJ_NAME.elf -smp 1 -serial tcp:127.0.0.1:$RPORT -net nic,addr=$IP_ADDR,macaddr=$MAC_ADDR -netdev tap,id=nd0,ifname=$NA,script=no,downscript=no
    fi
fi
